<?php

namespace app\site\controller\index;

use app\common\exception\MiException;
use app\model\store\GoodsCate;
use app\model\store\GoodsTag;
use app\site\controller\AController;
use app\site\middleware\CheckLogin;
use app\site\service\delivery\PostageTemplateService;
use app\site\service\SiteAuthService;
use app\site\service\store\GoodsCateService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class StoreController extends AController
{

    /**
     * 只需要验证登录，不需要验证权限的请求
     */
    protected array $middleware = [CheckLogin::class];


    protected function initialize()
    {

    }

    /**
     * 商品分类分类选择器选项
     * @param GoodsCateService $goodsCateService
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function goodsCates(GoodsCateService $goodsCateService): Json
    {
        $list = $goodsCateService->setStatus(1)->select();
        $tree = $goodsCateService->childrenTree($list);
        return jsonSuccess([
            'options' => $tree
        ]);
    }

    /**
     * 商品标签选择器选项
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws MiException
     */
    public function goodsTags(): Json
    {
        $list = GoodsTag::field('id,title')->where('status',1)->where('site_id', SiteAuthService::instance()->getSiteId())->select();
        return jsonSuccess([
            'options' => $list
        ]);
    }

}
